package cn.edu.nuc.thrid.services.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import cn.edu.nuc.thrid.dao.CartMapper;
import cn.edu.nuc.thrid.dao.GoodsMapper;
import cn.edu.nuc.thrid.dto.PageInfo;
import cn.edu.nuc.thrid.model.Goods;
import cn.edu.nuc.thrid.services.interfaces.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService {

	/**
	 * spring注解方式注入内容
	 */
	@Autowired
	private GoodsMapper mapper;

	@Autowired
	private CartMapper cmapper;

	/**
	 * 调用持久层返回数据进行业务逻辑判断
	 */
	@Override
	public List<Goods> goodlist() {

		List<Goods> list = mapper.findAll();
		return list;
	}

	@Override
	public Goods initgood(Integer zybgoodid) {

		Goods goods = mapper.selectByPrimaryKey(zybgoodid);
		return goods;
	}

	@Override
	public Integer updategood(Goods good) {

		Integer result = mapper.updateByPrimaryKey(good);

		return result;
	}

	@Transactional(propagation = Propagation.REQUIRED, // 事务传播行为
			noRollbackFor = { NullPointerException.class }, // 当发生这些异常时,不回滚事务
			rollbackFor = { SQLException.class }, // 当发生这些异常时,回滚事务
			timeout = 20, // 超时. 当此事务耗时达到20秒时,结束此事务
			isolation = Isolation.READ_COMMITTED // 设置隔离级别。 未设置以数据库隔离级别为准
	)
	@Override
	public Integer deletegood(Integer zybgoodid) {

		// 删除不仅仅删除了后台， 购物车里相应的也得删除
		cmapper.deleteBygoodid(zybgoodid);

		Integer result = mapper.deleteByPrimaryKey(zybgoodid);
		return result;
	}

	@Override
	public List<Goods> searchgood(String mes) {

		Goods good = new Goods();
		String goodname = "%" + mes + "%";
		good.setZybgoodid(mes);
		good.setZybgoodname(goodname);
		// System.out.println(good);
		List<Goods> list = mapper.search(good);
		return list;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, // 事务传播行为
			noRollbackFor = { NullPointerException.class }, // 当发生这些异常时,不回滚事务
			rollbackFor = { SQLException.class }, // 当发生这些异常时,回滚事务
			timeout = 20, // 超时. 当此事务耗时达到20秒时,结束此事务
			isolation = Isolation.READ_COMMITTED // 设置隔离级别。 未设置以数据库隔离级别为准
	)
	public Integer addGood(Goods good) {
		Integer result;
		Goods goods = mapper.findByname(good.getZybgoodname());
		if (goods == null) {

			result = mapper.addGood(good);
			return result;
		}
		if (goods.getZybgoodname().equals(good.getZybgoodname())) {

			return -1;
		} else {
			result = mapper.addGood(good);
			return result;
		}
	}

	@Override
	public Goods valiname(String zybgoodname) {

		Goods good = mapper.findByname(zybgoodname);
		return good;
	}

	/**
	 * 分页显示
	 */
	@Override
	public PageInfo pagelist(Integer current) {

		// 查询出记录总数
		if (current <= 0) {

			current = 1;
		}
		Integer count = mapper.count();

		PageInfo pageInfo = new PageInfo(current);
		List<Goods> list = mapper.pageAll(pageInfo.getStart(), pageInfo.getOffset());
		pageInfo.setList(list);
		pageInfo.setCount(count);
		// 返回封装分页的对象
		return pageInfo;
	}

}
